//////////////////////////////////////////////////////////////////////////
// FileName:SessionBox
// CreateBy:DragonXiong
// CreateDate:2009-5-31
// Description:
//		Class SessionBox
//      
// Modify Log:090602:Change Methods of Session--DragonXiong
//            090603:Add Function to redirect to timeout.aspx when Session User is null
//            090612:Add Property CurrentSystem
//	          091021:Change PageSize --Mark
//            100104:Change the ExitSys() Function --DragonXiong
//            100224:Give a default value to PageSize when null --DragonXiong
// Copyright:ASL-MIS
/////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Auto.Data.Security;
using Auto.Data.Setting;

namespace Auto.Web.Common
{
    /// <summary>
    ///  Used to Read,Write,Delete,Compare the Session.
    /// </summary>
    [Serializable]
    public class SessionBox
    {
        private SessionBox() { }

        #region User Session Operate

        /// <summary>
        /// Session User(Global)
        /// </summary>
        public static Users User
        {
            get
            {
                if (System.Web.HttpContext.Current.Session["USER"] == null)
                {
                    //Users users = new Users("admin");
                    ExitSys();
                    return null;
                    //return users;
                }
                else
                {
                    return (Users)System.Web.HttpContext.Current.Session["USER"];
                }
            }
            set
            {
                System.Web.HttpContext.Current.Session["USER"] = value;
            }
        }

        ///// <summary>
        ///// Check User Session Exist Or Not
        ///// </summary>
        ///// <returns></returns>
        //public static bool CheckUserSession()
        //{
        //    object o = System.Web.HttpContext.Current.Session["USER"];
        //    if (o == null)
        //        return false;
        //    else
        //        return true;

        //}

        ///// <summary>
        ///// Create User Session
        ///// </summary>
        ///// <param name="userinfo"></param>
        //public static void CreateUserSession(Users user)
        //{
        //    System.Web.HttpContext.Current.Session["USER"] = user;
        //}

        ///// <summary>
        ///// GET User Session
        ///// </summary>
        ///// <returns></returns>
        //public static Users GetUserSession()
        //{
        //    object o = System.Web.HttpContext.Current.Session["USER"];
        //    if (o == null) throw new ExceptionSession("Get UserSession Error.");
        //    else return (o as UserSession);
        //}

        /// <summary>
        /// Remove User Session
        /// </summary>
        public static void RemoveUserSession()
        {
            RemoveSession("USER");
        }

        #endregion


        #region --Culture Info--
        /// <summary>
        /// Culture Info(Demo:en-us,zh-cn...)
        /// </summary>
        public static string CultrueInfo
        {
            get
            {
                if (System.Web.HttpContext.Current.Session["CULTUREINFO"] == null)
                {
                    return null;
                }
                else
                {
                    return (String)System.Web.HttpContext.Current.Session["CULTUREINFO"];
                }
            }
            set
            {
                System.Web.HttpContext.Current.Session["CULTUREINFO"] = value;
            }
        }
        ///// <summary>
        ///// Check Culture Session Exist Or Not
        ///// </summary>
        ///// <returns></returns>
        //public static bool CheckCultureInfoSession()
        //{
        //    object o = System.Web.HttpContext.Current.Session["CULTUREINFO"];
        //    if (o == null)
        //        return false;
        //    else
        //        return true;

        //}

        ///// <summary>
        ///// Create CultureInfo Session
        ///// </summary>
        ///// <param name="userinfo"></param>
        //public static void CreateCultureInfoSession(string cultureInfo)
        //{
        //    System.Web.HttpContext.Current.Session["CULTUREINFO"] = cultureInfo;
        //}

        ///// <summary>
        ///// Get CultureInfo Session
        ///// </summary>
        ///// <returns></returns>
        //public static string GetCultureInfoSession()
        //{
        //    object o = System.Web.HttpContext.Current.Session["CULTUREINFO"];
        //    if (o == null) throw new Exception("Error When Read CultureInof Session.");
        //    else return (o as string);
        //}

        /// <summary>
        /// Remove CultureInfo Session
        /// </summary>
        public static void RemoveCultureInfoSession()
        {
            RemoveSession("CULTUREINFO");
        }
        #endregion --Culture Info--

        #region --Current System--
        /// <summary>
        /// 
        /// </summary>
        public static string CurrentSystem
        {
            get
            {
                if (System.Web.HttpContext.Current.Session["CURRENTSYSTEM"] == null)
                {
                    HttpContext.Current.Session["CURRENTSYSTEM"] = "BusMan";
                    return (String)System.Web.HttpContext.Current.Session["CURRENTSYSTEM"];
                }
                else
                {
                    return (String)System.Web.HttpContext.Current.Session["CURRENTSYSTEM"];
                }
            }
            set
            {
                System.Web.HttpContext.Current.Session["CURRENTSYSTEM"] = value;
            }
        }
        #endregion --Current System--

        #region --Page Front Setting--

        /// <summary>
        /// Menu Style(Classics,Simplicity)
        /// </summary>
        public static string MenuStyle
        {
            get
            {
                if (HttpContext.Current.Request.Cookies["SKIN"] == null)
                {
                    return "Menu";
                }
                else
                {
                    return HttpContext.Current.Request.Cookies["SKIN"].Value;
                }
            }
            set
            {
                HttpContext.Current.Response.Cookies["SKIN"].Value = value.ToString();
            }
        }

        /// <summary>
        /// GridView Rows per page
        /// </summary>
        public static int PageSize
        {
            get
            {
                try
                {
                    HttpCookie cookie = HttpContext.Current.Request.Cookies.Get("PAGESIZE");
                    //Get pagesize from cookie 
                    if (cookie != null)
                    {
                        //The current page has setting.
                        if (cookie[HttpContext.Current.Request.Path] != null)
                        {
                            return Convert.ToInt32(cookie[HttpContext.Current.Request.Path].ToString());
                        }
                        //Use the common setting.
                        else
                        {
                            return Convert.ToInt32(cookie["pageurl"].ToString());
                        }
                    }
                    //Get pagesize from database
                    else
                    {
                        
                        UserSettings us = new UserSettings(SessionBox.User.UserID, "PageSize");
                        
                        //Get pagesize from _UserSettings
                        if (!string.IsNullOrEmpty(us.SettingValue))
                        {
                            return Convert.ToInt32(us.SettingValue);
                        }
                        //Get pagesize from _Settings if no data in _UserSettings
                        else
                        {
                            Settings st = new Settings(SessionBox.CurrentSystem, "PageSize");
                            if (!string.IsNullOrEmpty(st.SettingValue))
                            {
                                return Convert.ToInt32(st.SettingValue);
                            }
                            else
                            {
                                return 0;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            set
            {
                try
                {   
                    // Write common pagesize to cookie.
                    HttpCookie hc = HttpContext.Current.Request.Cookies.Get("PAGESIZE");
                    if (hc == null)
                    {
                        hc = new HttpCookie("PAGESIZE");
                    }
                      // "/ASLBaseSystem/_System/SysFuncOperates.aspx"
                    hc["pageurl"] = value.ToString();
                    hc.Expires = DateTime.Now.AddDays(7);
                    HttpContext.Current.Response.AppendCookie(hc);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }


        /// <summary>
        /// Session Timeout Minutes(Default 15)
        /// </summary>
        public static int Timeout
        {
            get
            {
                if (HttpContext.Current.Request.Cookies["TIMEOUT"] == null)
                {
                    return 15;
                }
                else
                {
                    return Convert.ToInt32(HttpContext.Current.Request.Cookies["TIMEOUT"].Value);
                }
            }
            set
            {
                HttpContext.Current.Response.Cookies["TIMEOUT"].Value = value.ToString();
            }
        }

        /// <summary>
        /// Language type ( Default en-us:English)
        /// </summary>
        public static string Language
        {
            get
            {
                if (HttpContext.Current.Request.Cookies["LANGUAGE"] == null)
                {
                    return "en-us";
                }
                else
                {
                    return HttpContext.Current.Request.Cookies["LANGUAGE"].Value.ToString();
                }
            }
            set
            {
                HttpContext.Current.Response.Cookies["LANGUAGE"].Value = value;
            }
        }

        /// <summary>
        /// Table Style ( Default default)
        /// </summary>
        public static string TableSkin
        {
            get
            {
                if (HttpContext.Current.Request.Cookies["TABLESKIN"] == null)
                {
                    return "default";
                }
                else
                {
                    return HttpContext.Current.Request.Cookies["TABLESKIN"].Value.ToString();
                }
            }
            set
            {
                HttpContext.Current.Response.Cookies["TABLESKIN"].Value = value;
            }
        }
        #endregion --Front Settings --

        #region --Common Functions--
        /// <summary>
        /// Remove Session
        /// </summary>
        /// <param name="session">Session Name</param>
        private static  void RemoveSession(string session)
        {
            object o = System.Web.HttpContext.Current.Session[session];
            if (o != null)
            {
                System.Web.HttpContext.Current.Session.Remove(session);
            }
        }
        /// <summary>
        /// Exit System
        /// </summary>
        private static void ExitSys()
        {
            //HttpContext.Current.Session.Abandon();
            //HttpContext.Current.Response.Redirect("~/Timeout.aspx", true);
            string appPath = HttpContext.Current.Request.ApplicationPath;
            HttpContext.Current.Session.Abandon();
            if (appPath.Length > 1)
            {
                HttpContext.Current.Response.Redirect(appPath + "/Timeout.aspx", true);
            }
            else
            {
                HttpContext.Current.Response.Redirect("/Timeout.aspx", true);
            }
        }

        #endregion --Common Functions--

    }
}
